package com.fintech.third.service.interfacedata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.third.dao.interfacedata.EdcDataCallOperationRecordsMapper;
import com.fintech.third.dao.interfacedata.EdcProductInterfaceEffecttimeConfigMapper;
import com.fintech.third.model.interfacedata.EdcDataCallOperationRecordsDto;
import com.fintech.third.model.interfacedata.EdcProductInterfaceEffecttimeConfigDto;
import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.JSONObject;

@Service("dataCallOperationRecordsService")
public class DataCallOperationRecordsImpl implements DataCallOperationRecordsService{
	@Autowired
	private EdcProductInterfaceEffecttimeConfigMapper edcProductInterfaceEffecttimeConfigMapper;
	
	@Autowired
	private EdcDataCallOperationRecordsMapper edcDataCallOperationRecordsMapper;
	@Override
	public EdcDataCallOperationRecordsDto edcDataCallOperationRecordsInfo(Map<String, Object> map) {
		EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto=edcProductInterfaceEffecttimeConfigMapper.selectEdcProInterfaceTimeConfigSig(map);
		if(edcProductInterfaceEffecttimeConfigDto==null || (edcProductInterfaceEffecttimeConfigDto!=null && edcProductInterfaceEffecttimeConfigDto.getIsUser() && edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))){
			return null;
		}
		return edcDataCallOperationRecordsMapper.selectEdcDataCallOperationProvide(map);
	}
	

	@Override
	public int countEdcDataCallOperationRecord(Map<String, Object> map) {
		return edcDataCallOperationRecordsMapper.countEdcDataCallOperationRecord(map);
	}
	
	@Override
	public boolean isNeedData(String custIdCard, String mobile, List<String> interfaceCodes,String productType){
		Map<String, Object> map = new HashMap<>();
		StringBuffer interfaceCode = new StringBuffer();
		for (String code : interfaceCodes) {
			map.put("interfaceCode", code);
			map.put("productType", productType);
			EdcProductInterfaceEffecttimeConfigDto edcProductInterfaceEffecttimeConfigDto=edcProductInterfaceEffecttimeConfigMapper.selectEdcProInterfaceTimeConfigSig(map);
			if(edcProductInterfaceEffecttimeConfigDto==null || (edcProductInterfaceEffecttimeConfigDto!=null && edcProductInterfaceEffecttimeConfigDto.getIsUser() && edcProductInterfaceEffecttimeConfigDto.getValidityDate().equals("0"))){
				return true;
			}
			interfaceCode.append("'")
						.append(code)
						.append("'")
						.append(",");
		}
		String code = interfaceCode.substring(0, interfaceCode.lastIndexOf(","));
		
		map.put("mobile", mobile);
		map.put("list", interfaceCodes);
		int count=edcDataCallOperationRecordsMapper.countRecordGroupByInterfaceId(map);
		int length=interfaceCode.toString().split(",").length;
		MarvelsLogUtil.getInstance().info("=======isNeedData params:"+JSONObject.fromObject(map)+",count:"+count+",interfaceCode.length():"+length);
		if(count<length){
			return true;
		}
		// 判断有没需要的数据
		int countEdcDataCallOperationRecord = countEdcDataCallOperationRecord(map);
		return countEdcDataCallOperationRecord <= 0;
	}

}
